//
//  AskQuestionViewController.m
//  MagazineShangejie
//
//  Created by cnbizMedia on 15/9/24.
//  Copyright © 2015年 CnbizMedia. All rights reserved.
//

#import "AskQuestionViewController.h"
#import "Util.h"
@interface AskQuestionViewController ()
@property (weak, nonatomic) IBOutlet UITextField *questionTextField;

@end

@implementation AskQuestionViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



//开始编辑输入框的时候，软键盘出现，执行此事件
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect frame = textField.frame;
    int offset = 252;
//    frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//键盘高度216
    NSLog(@"offset = =%d",offset);
    
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    
    //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
//    if(offset > 0)
        self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
    
    [UIView commitAnimations];
}


//当用户按下return键或者按回车键，keyboard消失
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    
    
    [textField resignFirstResponder];
    return YES;
}

//输入框编辑完成以后，将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}


- (IBAction)back:(id)sender {
    if ([[self.navigationController viewControllers] count]>=2) {
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }else{
       
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    
}

//发送问题
- (IBAction)goToSend:(id)sender {
    
    if (_questionTextField.text.length < 5) {
        [SVProgressHUD showErrorWithStatus:@"请输入5个字以上的问题"];
    }
    else {
        [self sendTo];
    }
}

/*
 添加客服答疑:Answer/Add
 该接口提交必须附带参数 catid(栏目编号),id(项目编号)
 列表数据(论坛、游学)
 字段	类型	说明
 id	数字	项目id
 catid	数字	栏目id
 introduce	字符串	提问内容
 shouji	数字	手机号码
 os	数字	设备类型：1=IOS,2=Android
 返回结果样式
 {
 "code": 1,
 "message": "",
 "data": 172
 }
 */

- (void)sendTo{
    
    //2. 取出数据
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSDictionary *lunTanDic = [userDefault objectForKey:@"lunTanDic"];
    
    NSString *idString = [lunTanDic objectForKey:@"id"];
    NSString *catid    = [lunTanDic objectForKey:@"catid"];
    
//    NSDictionary *parameter=@{@"catid":catid,@"id":idString,@"introduce":_questionTextField.text,@"shouji":@"22222222222",@"os":@"1"};
    
    [[AppAPIClient sharedClient] POST:@"Answer/Add" parameters:@{@"catid":catid,@"id":idString,@"introduce":_questionTextField.text,@"shouji":@"22222222222",@"os":@"1"} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //                int code = [responseObject[@"code"] intValue];
        [SVProgressHUD showSuccessWithStatus:@"提交成功"];
        
        [self.navigationController popViewControllerAnimated:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:LOADING_TEXT_ERROR];
        NSLog(@"Error:%@ => %@",error,operation.responseString);
        [SVProgressHUD showErrorWithStatus:@"问题发送失败，请稍后重试"];
    }];
 

}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
